package com.example.shop.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop.common.api.ServiceReturn;
import com.example.shop.common.api.ServiceStateCode;
import com.example.shop.common.bo.Page;
import com.example.shop.mapper.GoodsMapper;
import com.example.shop.mapper.UserGoodsCollectionMapper;
import com.example.shop.pojo.Goods;
import com.example.shop.pojo.User;
import com.example.shop.pojo.UserGoodsCollection;
import com.example.shop.user.dao.GoodsCollectionDao;
import com.example.shop.user.dto.GoodsCollection;
import com.example.shop.user.service.GoodsCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodsCollectionServiceImpl implements GoodsCollectionService {

    private final GoodsMapper goodsMapper;
    private final GoodsCollectionDao goodsCollectionDao;
    private final UserGoodsCollectionMapper goodsCollectionMapper;

    public GoodsCollectionServiceImpl(GoodsMapper goodsMapper, GoodsCollectionDao goodsCollectionDao, UserGoodsCollectionMapper goodsCollectionMapper) {
        this.goodsMapper = goodsMapper;
        this.goodsCollectionDao = goodsCollectionDao;
        this.goodsCollectionMapper = goodsCollectionMapper;
    }

    @Override
    public ServiceReturn<Boolean> checkCollectionState(User user, long goodsId) {
        if(user == null){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,false);
        }

        QueryWrapper<UserGoodsCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId())
                .eq("goods_id",goodsId);
        boolean exists = goodsCollectionMapper.exists(queryWrapper);
        if(exists){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,true);
        }
        return new ServiceReturn<>(ServiceStateCode.SUCCESS,false);
    }

    @Override
    public List<GoodsCollection> browseCollection(User user, Page page) {
        return goodsCollectionDao.selectByPage(user.getUserId(),page);
    }

    @Override
    public ServiceReturn<Boolean> cancelCollection(User user, long goodsId) {

        QueryWrapper<UserGoodsCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId())
                .eq("goods_id",goodsId);
        int deleteRow = goodsCollectionMapper.delete(queryWrapper);
        if(deleteRow!=0){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,true);
        }
        return new ServiceReturn<>(ServiceStateCode.NO_SUCH_RECORD,false);
    }

    @Override
    public ServiceReturn<Boolean> addCollection(User user, long goodsId) {

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId);
        boolean exists = goodsMapper.exists(queryWrapper);
        if(!exists){
            return new ServiceReturn<>(ServiceStateCode.NO_SUCH_RECORD,false);
        }
        int changeRow = goodsCollectionMapper.insert(new UserGoodsCollection(user.getUserId(), goodsId, LocalDateTime.now()));
        if(changeRow!=0){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,true);
        }
        return new ServiceReturn<>(ServiceStateCode.FAILURE,false);
    }
}
